$(function () {
    //等待加载
    loading();
    var cookie = new sweetCookie();
    var appId = cookie.getCookie("appId");
    ajaxSelectOneAppAds_v(1, appId, "v");
    $(".vedioAD").click(function () {
        //等待加载
        loading();
        $(".swiperbox").hide();
        $("#void").show();
        $("#pic").hide();
        $(".picAD").removeClass("current");
        $(this).addClass("current");
        ajaxSelectOneAppAds_v(1, appId, "v");
    });
    $(".picAD").click(function () {
        //等待加载
        loading();
        $(".swiperbox").hide();
        //$(".recommendBox").empty();
        $("#void").hide();
        $("#pic").show();
        $(".vedioAD").removeClass("current");
        $(this).addClass("current");
        //var ad_type = "p";
        ajaxSelectOneAppAds_p(1, appId, "p");

    });
    oversize("productName", 18);
})
/**
 * 查询应用下广告
 * @param page 页数
 * @param appId 应用Id
 * @param ad_type 广告类型
 */
function ajaxSelectOneAppAds_v(page, appId, ad_type) {
    var url = "/Lvscore_User_M/myAds/ajaxSelectOneAppAds";
    $.post(url, {
        page: page,
        app_id: appId,
        ad_type: ad_type
    }, function (data) {
        $(".sliderbox").empty();
        $("#void").empty();
        $("#pic").empty();
        if (data.appAdsList.length == 0) {
            $(".swiperbox").hide();
            $("#void").html('<p class="noResult"><img src="/Lvscore_User_M/userWeb/static/images/dissearch.png" alt="">暂时没有图片广告</p>');
            $(".loadingBox").hide();
        } else {
            $(".swiperbox").show();
        }
        $.each(data.appAdsList, function (index, element) {
            if (!isNotEmpty(element.pc_size)) {
                element.pc_size ='未知';
            }
            if (!isNotEmpty(element.play_num)) {
                element.play_num ='0';
            }
            if (!isNotEmpty(element.duration)) {
                element.duration ='未知';
            }
            var videoFinish = "";
            if (element.finish == 1) {
                videoFinish += "<input type='button' app_id='" + appId + "' ad_id='" + element.ad_id + "' value='已播放' class='plays'/>";
            } else {
                //if(!isNotEmpty(element.ad_homepage)){
                //    element.ad_homepage="/Lvscore_User_M/userWeb/static/images/bm.png";
                //}
                var sliderbox = "";
                videoFinish += "<input type='button' app_id='" + appId + "' ad_id='" + element.ad_id + "' value='播放' class='plays'/>";
                sliderbox += '<a href="javascript:void(0)"> <img src="' + element.ad_homepage + '" class="bgImg"/>'
                    + '<span class="center"><span class="productName ADname">' + getTitle(element.ad_name) + '</span><b><img src="/Lvscore_User_M/userWeb/static/images/recommend_icon.png" alt="" title="">' + parseFloat(element.delivery_price).toFixed(2) + '</b></span>'
                    + '<div class="imgMask"></div>'
                    + '<div class="hover_show">'
                    + '<h4 class="ADname">' + getTitle(element.ad_name) + '</h4><span class="duration">' + element.duration
                    + '&quot;</span><p>有效期至：' + element.plan_end + '</p>'
                    + '<p><span class="infm">'
                    + '<img src="/Lvscore_User_M/userWeb/static/images/recommend_icon.png">'
                    + parseFloat(element.delivery_price).toFixed(2)
                    + '</span>'
                    + '<span class="infm">'
                    + '<img src="/Lvscore_User_M/userWeb/static/images/recommend_icon2.png">'
                    + element.pc_size + '</span><span class="infm">'
                    + '<img src="/Lvscore_User_M/userWeb/static/images/recommend_icon3.png">'
                    + element.play_num + '次</span></p>'
                    + '<p>' + videoFinish + '</p>'
                    + '</div></a>'
            }
            var video = "<li>" +
                "<span class='hintbg'></span>" +
                "<span class='center'><span class='productName ADname'>" + getTitle(element.ad_name) + "</span><b><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon.png' alt='' title=''>" + parseFloat(element.delivery_price).toFixed(2) + "</b></span>" +
                "<img src='" + element.ad_homepage + "' class='bjHover'>" +
                "<div class='backdrop'>" +
                "<div class='bj'></div>" +
                "<h2 class='ADname'>" + getTitle(element.ad_name) + "</h2>" +
                "<p>有效期至：" + element.plan_end + "</p>" +
                "<i>" + element.duration + "&quot;</i>" +
                "<ol>" +
                "<li><span><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon.png'></span>" + parseFloat(element.delivery_price).toFixed(2) + "</li>" +
                "<li><span><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon2.png'></span>" + element.pc_size + "</li>" +
                "<li><span><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon3.png'></span>" + element.play_num + "次</li>" +
                "</ol>" + videoFinish +
                "</div>" +
                "</li>";
            $("#void").append(video);

            $(".sliderbox").append(sliderbox);

            $(".recommendBox ul li").hover(function () {
                $(this).find(".backdrop").show();
            }, function () {
                $(this).find(".backdrop").hide()
            });
        });
        //没有未播放的视频广告时让轮播消失
        if($(".sliderbox").html()=="") {
            $(".swiperbox").hide();
        }
        slider();
        playAd();
        //等待加载的效果消失
        $(".loadingBox").hide();
        //分页
        if ($(".tcdPageCode").html() =="") {
            createPage(data.sumPage, data.nowPage, ad_type, appId, ajaxSelectOneAppAds_v);
        }

    }, "json");

}
function ajaxSelectOneAppAds_p(page, appId, ad_type) {
    var url = "/Lvscore_User_M/myAds/ajaxSelectOneAppAds";
    $.post(url, {
        page: page,
        app_id: appId,
        ad_type: ad_type
    }, function (data) {
        $(".sliderbox").empty();
        $("#void").empty();
        $("#pic").empty();
        if (data.appAdsList.length == 0) {
            $(".swiperbox").hide();
            $("#pic").html('<p class="noResult"><img src="/Lvscore_User_M/userWeb/static/images/dissearch.png" alt="">暂时没有图片广告</p>');
            $(".loadingBox").hide();
        } else {
            $(".swiperbox").show();
        }
        $.each(data.appAdsList, function (index, element) {
            //if(!isNotEmpty(element.ad_homepage)){
            //    element.ad_homepage="/Lvscore_User_M/userWeb/static/images/bm.png";
            //}
            if (!isNotEmpty(element.pc_size)) {
                element.pc_size ='未知';
            }
            if (!isNotEmpty(element.play_num)) {
                element.play_num ='0';
            }
            if (!isNotEmpty(element.duration)) {
                element.duration ='未知';
            }
                var picFinish = "";
                if (!isNotEmpty(element.ad_homepage)) {
                    element.ad_homepage = "/Lvscore_User_M/userWeb/static/images/bm.png";
                }
                if (element.finish == 1) {
                    picFinish += "<input type='button' app_id='" + appId + "' ad_type='pic' ad_id='" + element.ad_id + "' value='已播放' class='plays'/>";
                } else {
                    picFinish += "<input type='button' app_id='" + appId + "' ad_type='pic' ad_id='" + element.ad_id + "' value='播放' class='plays'/>";
                    var sliderbox = "";
                    sliderbox += '<a href="javascript:void(0)"> <img src="' + element.ad_homepage + '" class="bgImg"/>'
                        + '<span class="center"><span class="productName ADname">' + getTitle(element.ad_name) + '</span><b><img src="/Lvscore_User_M/userWeb/static/images/recommend_icon.png" alt="" title="">' + parseFloat(element.delivery_price).toFixed(2) + '</b></span>'
                        + '<div class="imgMask"></div>'
                        + '<div class="hover_show">'
                        + '<h4 class="ADname">' + getTitle(element.ad_name) + '</h4><span class="duration">' + element.duration + '张</span>'
                        + '<p>有效期至：' + element.plan_end + '</p>'
                        + '<p><span class="infm">'
                        + '<img src="/Lvscore_User_M/userWeb/static/images/recommend_icon.png">'
                        + parseFloat(element.delivery_price).toFixed(2)
                        + '</span>'
                        + '<span class="infm">'
                        + '<img src="/Lvscore_User_M/userWeb/static/images/recommend_icon2.png">'
                        + element.pc_size + '</span><span class="infm">'
                        + '<img src="/Lvscore_User_M/userWeb/static/images/recommend_icon3.png">'
                        + element.play_num + '次</span></p>'
                        + '<p>' + picFinish + '</p>'
                        + '</div></a>'
                }
                var s = "<li>" +
                    "<span class='hintbg'></span>" +
                    "<span class='center'><span class='productName ADname'>" + getTitle(element.ad_name) + "</span><b><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon.png' alt='' title=''>" + parseFloat(element.delivery_price).toFixed(2) + "</b></span>" +
                    "<img src='" + element.ad_homepage + "' class='bjHover' width='235px' height='135px'>" +
                    "<div class='backdrop'>" +
                    "<div class='bj'></div>" +
                    "<h2 class='ADname'>" + getTitle(element.ad_name) + "</h2>" +
                    "<p>有效期至：" + element.plan_end + "</p>" +
                    "<i>" + element.duration + "张</i>" +
                    "<ol>" +
                    "<li><span><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon.png'></span>" + parseFloat(element.delivery_price).toFixed(2) + "</li>" +
                    "<li><span><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon2.png'></span>" + element.pc_size + "</li>" +
                    "<li><span><img src='/Lvscore_User_M/userWeb/static/images/recommend_icon3.png'></span>" + element.play_num + "次</li>" +
                    "</ol>" +
                    picFinish
                s += "</div>" +
                    "</li>";
                $("#pic").append(s);
                $(".sliderbox").append(sliderbox);
            //没有未播放的视频广告时让轮播消失
                if($(".sliderbox").html() == ""){
                    $(".swiperbox").hide();
                }
                $(".recommendBox ul li").hover(function () {
                    $(this).find(".backdrop").show();
                }, function () {
                    $(this).find(".backdrop").hide()
                });
        });
        slider();
        playAd();
        //等待加载的效果消失
        $(".loadingBox").hide();
        //分页
        if ($(".tcdPageCode").html() == "") {
            createPage(data.sumPage, data.nowPage, ad_type, appId, ajaxSelectOneAppAds_p);
        }

    }, "json");

}

//轮播
function slider() {
    if (isNotEmpty($('.sliderbox').text())) {
        var slidermodel = new sliderModel($('.sliderbox'), $('.sliderbox a'), $('.lbtn'), $('.rbtn'));
        var slider = new Sliders(slidermodel, false, 2000, 500, "red", "green");
        slider.go();
        $(".sliderbox a").hover(function () {
            $(this).find(".imgMask").show();
            $(this).find(".hover_show").show();
        }, function () {
            $(this).find(".imgMask").hide();
            $(this).find(".hover_show").hide()
        });
    }
};
/**
 * 分页
 * @param pages当前页数
 * @param pageNum总条数
 * @param ad_type类型
 * @returns
 */
function createPage(pages, pageNum, ad_type, appId, fn) {
    if (pages > 1) {
        $(".tcdPageCode").createPage({
            pageCount: pages,
            current: pageNum,
            backFn: function (p) {
                fn(p, appId, ad_type);
            }
        });
    }
}
